﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Hfm.Common.Entity.Parameters;

namespace Hfm.Core.DataAccess.Parameters
{
    public class ParametersData
    {
        protected readonly CoreContext Context;

        public ParametersData(CoreContext coreContext)
        {
            Context = coreContext;
        }

        /// <summary>
        /// 获取所有Parameters
        /// </summary>
        /// <returns>Parameters</returns>
        public virtual ParametersEntity GetParameter()
        {
            DateTime dt = DateTime.Now.Date;
            var query = from p in Context.ParametersEntityes
                        where p.ParamDate > dt
                        select p;
            return query.SingleOrDefault();
        }


        /// <summary>
        /// 创建或更新Parameters
        /// </summary>
        /// <param name="paramName">ParametersName</param>
        /// <param name="paramValue">ParametersValue</param>
        public virtual int SetValue()
        {
            ParametersEntity param = GetParameter();
            if (null == param)
            {
                param = new ParametersEntity();
                param.ParamName = "number";
                param.ParamValue = 1;
                param.ParamDate = DateTime.Now;
                CreateParameters(param);
            }
            else
            {
                param.ParamValue += 1;
                Context.Entry(param).State = System.Data.EntityState.Modified;
                Context.SaveChanges();
            }
            return param.ParamValue;
        }

        /// <summary>
        /// 创建Parameters
        /// </summary>
        /// <param name="parameters">Parameters</param>
        public virtual void CreateParameters(ParametersEntity parameters)
        {
            Context.ParametersEntityes.Add(parameters);
            Context.SaveChanges();
        }

    }
}
